package com.lottery.im.module.login.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.module.login.presenter.RegisterPresenter;
import com.lottery.im.module.login.view.RegisterView;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.dialog.ConfirmWarnDialogBuilder;

/*
 * 注册
 * */
public class RegisterFragment extends BaseSwipeFragment<RegisterPresenter> implements RegisterView {

  @BindView(R.id.et_phone) EditText mEtPhone;
  @BindView(R.id.et_code) EditText mEtCode;
  @BindView(R.id.tv_get_code) TextView mTvGetCode;
  @BindView(R.id.et_invite_code) EditText mEtInviteCode;

  private int mCountdownTime = Constants.COUNTDOWN_TIME;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_register;
  }

  @Override protected void initPresenter() {
    presenter = new RegisterPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {

    StatusBarHelper.setStatusBarLightMode(getActivity());
  }

  @OnClick({ R.id.iv_back, R.id.tv_get_code, R.id.btn_next })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        back();
        break;
      case R.id.tv_get_code:
        getCode();
        break;
      case R.id.btn_next:
        toNext();
        break;
    }
  }

  private void back() {
    ConfirmWarnDialogBuilder.getInstance(mActivity)
        .setContent(getString(R.string.confirm_give_up_reg))
        .setCancel(getString(R.string.cancel), view -> {

        })
        .setConfirm(getString(R.string.confirm), view -> popBackStack())
        .show();
  }

  /*
   * 获取验证码
   * */
  private void getCode() {
    String phone = mEtPhone.getText().toString().trim();

    if (TextUtils.isEmpty(phone)) {
      showToast(R.string.hint_enter_phone);
      return;
    }
    hideKeyBoard();
    presenter.getAttach(phone);
  }

  /*
   *  下一页
   * */
  private void toNext() {
    String phone = mEtPhone.getText().toString().trim();
    if (TextUtils.isEmpty(phone)) {
      showToast(R.string.hint_enter_phone);
      return;
    }
    String code = mEtCode.getText().toString().trim();
    if (TextUtils.isEmpty(code)) {
      showToast(R.string.hint_enter_code);
      return;
    }
    String inviteCode = mEtInviteCode.getText().toString().trim();
    if (TextUtils.isEmpty(inviteCode)) {
      showToast(R.string.hint_enter_invite_code);
      return;
    }

    startFragmentAndDestroyCurrent(RegisterPwdFragment.create(phone, code, inviteCode));
  }

  /*
   *  获取验证码成功
   * */
  @Override public void getCodeSuccess() {

    showToast(R.string.message_send_success);
    mTvGetCode.setEnabled(false);
    mTvGetCode.setText("重新获取" + mCountdownTime + "s");
    mTvGetCode.postDelayed(mCountdownRunnable, 1000);
  }

  /*
   *  倒计时
   * */
  private Runnable mCountdownRunnable = new Runnable() {
    @Override public void run() {

      if (null == mActivity || mActivity.isFinishing()) {
        return;
      }
      mCountdownTime--;
      if (mCountdownTime == 0) {
        mCountdownTime = Constants.COUNTDOWN_TIME;
        mTvGetCode.setEnabled(true);
        mTvGetCode.setText(getResources().getString(R.string.get_code));
      } else {
        mTvGetCode.setText("重新获取" + mCountdownTime + "s");
        mTvGetCode.postDelayed(this, 1000);
      }
    }
  };
}
